package cn.bdqn.layui.service.menu;

import cn.bdqn.layui.entity.Bar;
import cn.bdqn.layui.entity.Menu;
import cn.bdqn.layui.entity.vo.MenuRoleVO;
import cn.bdqn.layui.mapper.MenuMapper;
import org.springframework.security.authentication.AnonymousAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Service
public class MenuServiceImpl implements MenuService {

    @Resource
    MenuMapper menuMapper;

    /**
     * 分配权限
     *
     * @return
     */
    @Override
    public List<Bar> findMenuRoleAll() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        List<Menu> menuList = new ArrayList<>();
        List<Menu> list = new ArrayList<>();
        List<Bar> bars = new ArrayList<>();
        if (!(authentication instanceof AnonymousAuthenticationToken)) {
            Object[] roles = authentication.getAuthorities().toArray();
            for (Object o : roles) {
                menuList = menuMapper.findMenuRoleAll(o.toString());
                list.addAll(menuList);
            }
        }
        Set<Menu> set = new HashSet<>(list);
        List<Menu> menus = new ArrayList<>(set);
        for (Menu menu : menus) {
            Bar bar = new Bar();
            bar.setId(menu.getId());
            bar.setMenuName(menu.getMenuName());
            bar.setUrl(menu.getUrl());
            bar.setParentId(menu.getParentId());
            //查询二级菜单
            List<Menu> byIdToParentID = menuMapper.findByIdToParentId(menu.getId());
            bar.setMenus(byIdToParentID);
            bars.add(bar);
        }
        return bars;
    }

    @Override
    public List<Menu> getMenus() {
        return menuMapper.getMenus();
    }

    @Override
    public List<Menu> findMenuPage(String menuName, Integer pageNo, Integer pageSize) {
        return menuMapper.findMenuPage(menuName, pageNo, pageSize);
    }

    @Override
    public List<Menu> findByIdToParentId(Integer id) {
        return menuMapper.findByIdToParentId(id);
    }

    @Override
    public int addMenuRole(Integer rid, Integer mid) {
        return menuMapper.addMenuRole(rid, mid);
    }

    @Override
    public List<Menu> findByRidMenu(Integer rid) {
        return menuMapper.findByRidMenu(rid);
    }

    @Override
    public Menu findMenuById(Integer id) {
        return menuMapper.findMenuById(id);
    }

    @Override
    public int updateMenu(Menu menu) {
        return menuMapper.updateMenu(menu);
    }

    @Override
    public int addMeun(Menu menu) {
        return menuMapper.addMeun(menu);
    }

    @Override
    public int delMeunRole(Integer rid, Integer mid) {
        return menuMapper.delMeunRole(rid, mid);
    }

    @Override
    public List<MenuRoleVO> findMenuRoleRid(Integer rid, Integer mid) {
        return menuMapper.findMenuRoleRid(rid, mid);
    }

    @Override
    public int delMenu(Integer id) {
        return menuMapper.delMenu(id);
    }

    @Override
    public int findMenuPageCount(String menuName) {
        return menuMapper.findMenuPageCount(menuName);
    }
}
